#define _CRT_SECURE_NO_WARNINGS 1

#include "Stack.h"

void STInit(ST* pst)
{
	assert(pst);

	pst->arr = NULL;
	pst->capacity = pst->top = 0;
}

void STDestory(ST* pst)
{
	assert(pst);

	free(pst->arr);
	pst->arr = NULL;
	pst->top = pst->capacity = 0;
}


void STPush(ST* pst, STDataType x)
{
	assert(pst);

	if (pst->capacity == pst->top)
	{
		int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(pst->arr,sizeof(STDataType) * newcapacity);
		if (tmp == NULL)
		{
			perror("ralloc fail");
			return;
		}
		pst->arr = tmp;
		pst->capacity = newcapacity;
	}

	pst->arr[pst->top] = x;
	pst->top++;
}

void STPop(ST* pst)
{
	assert(pst);
	assert(!STEmpty(pst));

	pst->top--;
}

STDataType STTop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);

	return pst->arr[pst->top - 1];
}

int STSize(ST* pst)
{
	assert(pst);
	
	return pst->top;
}

int STEmpty(ST* pst)
{
	assert(pst);

	return pst->top == 0;
}